package com.micro.train.controller;

import com.micro.train.entity.TrainSeat;
import com.micro.train.service.TrainSeatService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 火车座位控制器
 */
@Tag(name = "火车座位管理", description = "火车座位相关接口")
@RestController
@RequestMapping("/api/seats")
public class TrainSeatController {
    
    @Autowired
    private TrainSeatService seatService;
    
    @Operation(summary = "添加座位信息")
    @PostMapping
    public ResponseEntity<TrainSeat> addSeat(@RequestBody TrainSeat seat) {
        TrainSeat savedSeat = seatService.addSeat(seat);
        return ResponseEntity.ok(savedSeat);
    }
    
    @Operation(summary = "批量添加座位")
    @PostMapping("/batch")
    public ResponseEntity<List<TrainSeat>> addSeats(@RequestBody List<TrainSeat> seats) {
        List<TrainSeat> savedSeats = seatService.addSeats(seats);
        return ResponseEntity.ok(savedSeats);
    }
    
    @Operation(summary = "更新座位信息")
    @PutMapping("/{seatId}")
    public ResponseEntity<TrainSeat> updateSeat(
            @PathVariable Long seatId,
            @RequestBody TrainSeat seat) {
        TrainSeat updatedSeat = seatService.updateSeat(seatId, seat);
        return ResponseEntity.ok(updatedSeat);
    }
    
    @Operation(summary = "删除座位信息")
    @DeleteMapping("/{seatId}")
    public ResponseEntity<Void> deleteSeat(@PathVariable Long seatId) {
        seatService.deleteSeat(seatId);
        return ResponseEntity.noContent().build();
    }
    
    @Operation(summary = "购买车票")
    @PostMapping("/purchase")
    public ResponseEntity<Long> purchaseTicket(
            @RequestParam("trainId") Long trainId,
            @RequestParam("seatType") Integer seatType,
            @RequestParam("quantity") Integer quantity,
            @RequestParam("travelDate") String travelDate) {
        try {
            System.out.println("DEBUG - 收到购买请求:");
            System.out.println("  trainId: " + trainId);
            System.out.println("  seatType: " + seatType);
            System.out.println("  quantity: " + quantity);
            System.out.println("  travelDate: " + travelDate);
            
            LocalDate date = LocalDate.parse(travelDate);
            System.out.println("  parsed date: " + date);
            
            Long orderId = seatService.purchaseTicket(trainId, seatType, quantity, date);
            System.out.println("  purchase result: " + orderId);
            
            return ResponseEntity.ok(orderId);
        } catch (Exception e) {
            System.err.println("ERROR - 购买失败:");
            System.err.println("  error: " + e.getMessage());
            e.printStackTrace();
            return ResponseEntity.badRequest().build();
        }
    }

    @Operation(summary = "获取所有座位信息")
    @GetMapping
    public ResponseEntity<List<TrainSeat>> getAllSeats() {
        List<TrainSeat> seats = seatService.getAllSeats();
        return ResponseEntity.ok(seats);
    }
    
    @Operation(summary = "根据ID获取座位信息")
    @GetMapping("/{seatId}")
    public ResponseEntity<Map<String, Object>> getSeatById(@PathVariable Long seatId) {
        TrainSeat seat = seatService.getSeatById(seatId);
        if (seat == null) {
            return ResponseEntity.notFound().build();
        }
        
        Map<String, Object> seatMap = new HashMap<>();
        seatMap.put("座位ID", seat.getSeatId());
        seatMap.put("列车ID", seat.getTrainId());
        seatMap.put("车厢ID", seat.getCarriageId());
        seatMap.put("座位号", seat.getSeatNumber());
        seatMap.put("座位类型", seat.getSeatType());
        seatMap.put("价格", seat.getPrice());
        
        return ResponseEntity.ok(seatMap);
    }
    
    @Operation(summary = "根据列车ID获取所有座位")
    @GetMapping("/train/{trainId}")
    public ResponseEntity<List<TrainSeat>> getSeatsByTrainId(@PathVariable Long trainId) {
        List<TrainSeat> seats = seatService.getSeatsByTrainId(trainId);
        return ResponseEntity.ok(seats);
    }
    
    @Operation(summary = "根据车厢ID获取所有座位")
    @GetMapping("/carriage/{carriageId}")
    public ResponseEntity<List<TrainSeat>> getSeatsByCarriageId(@PathVariable Long carriageId) {
        List<TrainSeat> seats = seatService.getSeatsByCarriageId(carriageId);
        return ResponseEntity.ok(seats);
    }
    
    @Operation(summary = "根据座位类型获取座位")
    @GetMapping("/type/{seatType}")
    public ResponseEntity<List<TrainSeat>> getSeatsByType(@PathVariable Integer seatType) {
        List<TrainSeat> seats = seatService.getSeatsByType(seatType);
        return ResponseEntity.ok(seats);
    }
    
    @Operation(summary = "根据列车ID和座位类型获取座位")
    @GetMapping("/train/{trainId}/type/{seatType}")
    public ResponseEntity<List<TrainSeat>> getSeatsByTrainIdAndType(
            @PathVariable Long trainId,
            @PathVariable Integer seatType) {
        List<TrainSeat> seats = seatService.getSeatsByTrainIdAndType(trainId, seatType);
        return ResponseEntity.ok(seats);
    }
    
    @Operation(summary = "根据价格范围查询座位")
    @GetMapping("/price-range")
    public ResponseEntity<List<TrainSeat>> getSeatsByPriceRange(
            @RequestParam BigDecimal minPrice,
            @RequestParam BigDecimal maxPrice) {
        List<TrainSeat> seats = seatService.getSeatsByPriceRange(minPrice, maxPrice);
        return ResponseEntity.ok(seats);
    }
}
